const { Paragraph, TextRun, Tab, TabStopPosition, TabStopType, ShadingType } = require("docx");
const { getAlignment, getIndentValue, generateUnderlineText, getPrefixSymbol, mapFontSizeToHalfPoints } = require("../common/utils");

// 生成横向选项字段
function generateHorizontalOptions(field, baseIndent = 0) {
    const value = field.value || {};
    const fieldName = value.fieldName || '';
    const options = value.options || [];
    const prefixSymbol = value.prefixSymbol || 'none';
    const alignment = value.alignment || 'left';
    const sizeHalfPoints = mapFontSizeToHalfPoints(value.fontSize);
    const isBold = value.isBold || false;
    
    // 计算最终缩进值
    const finalIndent = getIndentValue(alignment, baseIndent);
    
    // 创建子元素数组
    const children = [];
    
    // 添加前缀符号
    const symbol = getPrefixSymbol(prefixSymbol);
    if (symbol) {
        children.push(new TextRun({
            text: symbol,
            size: sizeHalfPoints,
            font: "SimSun" // 宋体
        }));
    }
    
    // 添加字段名称
    if (fieldName.trim()) {
        children.push(new TextRun({
            text: fieldName,
            size: sizeHalfPoints,
            bold: isBold,
            font: "SimSun"
        }));
    }
    
    // 处理选项
    if (options.length === 0) {
        // 仅使用字符复选框，无底纹
        children.push(new TextRun({
            text: "☐",
            size: sizeHalfPoints + 5,
            font: "SimSun"
        }));
        children.push(new TextRun({ text: ' ', size: sizeHalfPoints, font: 'SimSun' }));
    } else {
        // 渲染每个选项
        options.forEach((option, index) => {
            // 仅使用字符复选框，无底纹
            children.push(new TextRun({
                text: "☐",
                size: sizeHalfPoints + 5,
                font: "SimSun"
            }));
            children.push(new TextRun({ text: ' ', size: sizeHalfPoints, font: 'SimSun' }));
            
            // 添加选项文本
            if (option.text && option.text.trim()) {
                children.push(new TextRun({
                    text: option.text,
                    size: sizeHalfPoints,
                    font: "SimSun"
                }));
            }
            
            // 添加下划线（如果启用）
            if (option.showLine) {
                const lineLength = Math.max(1, option.lineLength || 10);
                const underlineText = generateUnderlineText(lineLength);
                
                children.push(new TextRun({
                    text: underlineText,
                    size: sizeHalfPoints,
                    font: "SimSun"
                }));
            }
            
            // 在选项之间添加适当的间距（除了最后一个选项）
            if (index < options.length - 1) {
                children.push(new TextRun({
                    text: '　　', // 使用全角空格提供更好的间距
                    size: sizeHalfPoints,
                    font: "SimSun"
                }));
            }
        });
    }
    
    // 创建段落
    const paragraph = new Paragraph({
        indent: { left: finalIndent },
        alignment: getAlignment(value.labelAlign || alignment),
        spacing: { 
            before: 100, 
            after: 100,
            line: 360, // 行间距
            lineRule: "atLeast"
        },
        children: children
    });
    
    return [paragraph];
}

module.exports = {
    generateHorizontalOptions
}; 